package com.le.tester.javaThink.fifth.chapter24_ConcurrentProgramming;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * createTime：2021/8/23 14:44
 * description：StickHolder 哲学家抢筷子
 */
public class StickHolder {
    private static class ChopStick {

    }

    private ChopStick stick = new ChopStick();

    //把筷子放进一个队列中
    private BlockingQueue<ChopStick> holder =
            new ArrayBlockingQueue<>(1);

    public StickHolder() {
        putDown();
    }

    public void pickUp() {
        try {
            holder.take();
        } catch (InterruptedException e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    public void putDown() {
        try {
            holder.put(stick);
        } catch (InterruptedException e) {
            throw new RuntimeException(e.getMessage());
        }
    }
}
